package nguyen.hoan.sharelocation.adapter;

import java.util.List;

import nguyen.hoan.sharelocation.R;
import nguyen.hoan.sharelocation.model.NavDrawerGroup;
import nguyen.hoan.sharelocation.model.NavDrawerItem;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

public class NavDrawerListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private LayoutInflater inflater;
	private List<NavDrawerGroup> navDrawerGroups;

	public NavDrawerListAdapter(Context context, List<NavDrawerGroup> navDrawerGroups) {
		this.context = context;
		this.inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		this.navDrawerGroups = navDrawerGroups;
	}

	@Override
	public Object getChild(int groupPositon, int childPosition) {

		return navDrawerGroups.get(groupPositon).getChildren().get(childPosition);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.fragment_navigation_item, null);
		}

		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
		TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

		NavDrawerItem child = navDrawerGroups.get(groupPosition).getChildren().get(childPosition);

		imgIcon.setImageResource(child.getIcon());
		txtTitle.setText(child.getTitle());
//		convertView.setBackgroundColor(child.getBackgroundColor());
//		convertView.setBackgroundColor(child.getBackgroundColor());
		convertView.setTag(child);

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return navDrawerGroups.get(groupPosition).getChildren().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return navDrawerGroups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return navDrawerGroups.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.fragment_navigation_group, null);
		}

		NavDrawerGroup group = navDrawerGroups.get(groupPosition);
//		convertView.setBackgroundColor(group.getBackgroundColor());
		convertView.setTag(group);
		CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.checkedTextView);		
		checkedTextView.setText(group.getTitle());
		Drawable img = context.getResources().getDrawable(group.getIcon());
		img.setBounds(0, 0, 120, 120);
		checkedTextView.setCompoundDrawables(img, null, null, null);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}
}
